﻿using CustomCharacter;
using CustomCharacter.Ability;
using Exiled.API.Enums;
using Exiled.API.Features;
using Exiled.API.Features.Pools;
using Exiled.Events.EventArgs.Interfaces;
using Exiled.Events.EventArgs.Player;
using System.Collections.Generic;
using System.Linq;
using CustomCharacter.Interfaces;
using UnityEngine;


namespace Scp920
{
    [RoleInfo("SCP-920", PlayerRoles.RoleTypeId.ClassD, PlayerRoles.RoleTypeId.ClassD)]
    public class Scp920 : CustomCharacter.CustomCharacter, IRoundStartSpawn, IScpNumber, ITrialCard
    {
        public Scp920(int id) : base(id)
        {
        }

        private static readonly List<RoomType> BannedRoomType = new List<RoomType>()
        { RoomType.Lcz173, RoomType.Hcz079, RoomType.Hcz106, RoomType.LczArmory, RoomType.HczArmory, RoomType.HczTesla, RoomType.Pocket, RoomType.Surface };

        public override Color32 RoleColor => new Color32(0, 250, 154, 255);

        public byte MinPlayers => 10;

        public string ScpNumber => "920";

        public int Cost => 350;

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new Ability("Scp920_Passive_1",
    "拾起物品时，有10%的概率将自身和附近2m玩家传送到任意一个门或人类玩家身边", AbilityType.Passive, 60, Passive_Ability1,
    null, new IExiledEvent[] { Exiled.Events.Handlers.Player.PickingUpItem }, null, GetType())
            };
        }

        private static void Teleport(Player[] Player, bool nonLcz = false)
        {
            Room room;
            while (true)
            {
                room = Room.List.ElementAt(UnityEngine.Random.Range(0, Room.List.Count()));
                if (nonLcz && room.Zone == ZoneType.LightContainment)
                {
                    continue;
                }
                if (!BannedRoomType.Contains(room.Type))
                {
                    break;
                }
            }
            foreach (Player player in Player)
            {
                player.Position = room.Doors.ElementAt(0).Position + (Vector3.up * 1.5f);
            }
        }

        private static AbilityOutput Passive_Ability1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (trigger != AbilityTrigger.Event)
                return new AbilityOutput(false);
            if (e is PickingUpItemEventArgs ev && ev.Player == player && UnityEngine.Random.Range(0, 10) == 1)
            {
                Player[] players = Player.List.Where(x => x.IsAlive && Vector3.Distance(x.Position, player.Position) < 2f).ToArray();
                if (UnityEngine.Random.Range(0, 100) > 50 || Warhead.IsDetonated)
                {
                    List<Player> avaPlayers = Player.List.Where(x => x.IsHuman).ToList();
                    var newpos = avaPlayers[UnityEngine.Random.Range(0, avaPlayers.Count)].Position + (Vector3.up * 1.5f);
                    foreach (var px in players)
                        px.Position = newpos;
                    ListPool<Player>.Pool.Return(avaPlayers);
                }
                else
                {
                    int percent = player.Zone == ZoneType.LightContainment ? 20 : 50;
                    Teleport(players, UnityEngine.Random.Range(0, 100) > 20);
                }
                return new AbilityOutput(true);
            }
            return new AbilityOutput(false);
        }
    }
}